package com.jsecode.protocol.gid22.b2b;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsecode.protobuf.VehicleCan;
import com.jsecode.protobuf.VehicleCan.Can;
import com.jsecode.protocol.gid22.GidCan;
import com.jsecode.protocol.gid22.entity.GidDSFlag;

/**
 * bean to bean util
 * 
 * @author luodan
 *
 */
public class GidB2BUtil {
    protected static Logger logger = LoggerFactory.getLogger("gid22-GidB2BUtil");

    public static List<Can> toProtoCan(String deviceId, GidCan can) {

        List<Can> list = new ArrayList<Can>();
        if (can.getDsFlagList() == null || can.getDsFlagList().isEmpty()) {
            logger.warn("toDeviceStatusVehicle(): can is empty for deviceId: {}", deviceId);
            return null;
        }
        /** 车辆can  */
        for (GidDSFlag dsFlag:can.getDsFlagList()) {
            Can.Builder vehicleStatus = Can.newBuilder();
            vehicleStatus.setDeviceId(deviceId);
            vehicleStatus.setSampleTime(can.getTime().getTime()/1000);
            vehicleStatus.setDistanceTotal(dsFlag.mileage);
            vehicleStatus.setTotalFuelConsumption(dsFlag.totalFuelConsumption);
            vehicleStatus.setVehicleSpeed(dsFlag.vehiclSpeed);
            vehicleStatus.setEngineRpm(dsFlag.rpm);
            vehicleStatus.setEngineCoolliquidTemp(dsFlag.coolTemp);
            vehicleStatus.setStorageBatteryVoltage(dsFlag.voltage);
            vehicleStatus.setCalcuLoad(dsFlag.engineLoad);
            
            // Added on 2014.11.24
            vehicleStatus.setMalLightStatus((int)dsFlag.miStatus);
            vehicleStatus.setDtcNum(dsFlag.dtcEngine);
            vehicleStatus.setObdType((int)dsFlag.obdType);

            vehicleStatus.setIntakeTemp((float)dsFlag.intakeTemp);
            vehicleStatus.setEnvTemp((float)dsFlag.enTemp);
            
            vehicleStatus.setFuelPress(dsFlag.fuelPres);
            vehicleStatus.setBaroPress(dsFlag.baroPres);
            vehicleStatus.setAirFlowSensor(dsFlag.airFlowSensor);
            vehicleStatus.setThrotleSensor(dsFlag.throtleSensor);
            vehicleStatus.setPedalPos(dsFlag.pedalPos);
            
            vehicleStatus.setEngineRunTime(dsFlag.engineRunTime);
            vehicleStatus.setFaultMileage(dsFlag.faultMileage);
            vehicleStatus.setOilMass(dsFlag.oilMass);
            vehicleStatus.setLongFuelTrim(dsFlag.longFuelTrim);
            vehicleStatus.setSparkAngle(dsFlag.sparkAngle);

            vehicleStatus.setTireLF(dsFlag.tireLF);
            vehicleStatus.setTireRF(dsFlag.tireRF);
            vehicleStatus.setTireLB(dsFlag.tireLB);
            vehicleStatus.setTireRB(dsFlag.tireRB);
            vehicleStatus.setMileageType((int)dsFlag.mileageType);
            
            logger.info("create DeviceStatusVehicle, id:" + deviceId + " sampleTime:" + can.getTime());
            list.add(vehicleStatus.build());
        }
        
        return list;
        
    }
}
